﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using UnityEngine;

public class _31 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        MyListA list = new MyListA();
        var temp = (from c in list where c.Age == 20 select c).ToList();
        Debug.LogError(list.IteratedNum.ToString());
        list.IteratedNum = 0;
        var temp2 = (from c in list where c.Age >= 20 select c).First();
        Debug.LogError(list.IteratedNum.ToString());
    }

    // Update is called once per frame
    void Update()
    {

    }
}

public class MyListA : IEnumerable<PersonB>
{
    List<PersonB> list = new List<PersonB>()
    {
        new PersonB(){Name="Mike",Age=20 },
        new PersonB(){Name="Mike",Age=30 },
        new PersonB(){Name="Rose",Age=25 },
        new PersonB(){Name="Steve",Age=30 },
        new PersonB(){Name="Jessica",Age=20 },
    };

    public int IteratedNum { get; set; }

    public PersonB this[int i]
    {
        get { return list[i]; }
        set { this.list[i] = value; }
    }

    public IEnumerator<PersonB> GetEnumerator()
    {
        foreach (var item in list)
        {
            IteratedNum++;
            yield return item;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class PersonB
{
    public string Name { get; set; }
    public int Age { get; set; }
}
